//// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.details
//
//import com.intellij.collaboration.messages.CollaborationToolsBundle
//import com.intellij.collaboration.ui.SimpleHtmlPane
//import com.intellij.collaboration.ui.codereview.details.*
//import com.intellij.collaboration.ui.util.emptyBorders
//import com.intellij.collaboration.ui.util.gap
//import com.intellij.openapi.actionSystem.ActionGroup
//import com.intellij.openapi.actionSystem.ActionManager
//import com.intellij.openapi.actionSystem.ActionPlaces
//import com.intellij.openapi.actionSystem.ex.ActionUtil
//import com.intellij.openapi.project.Project
//import com.intellij.ui.PopupHandler
//import com.intellij.ui.components.ActionLink
//import kotlinx.coroutines.CoroutineScope
//import net.miginfocom.layout.AC
//import net.miginfocom.layout.CC
//import net.miginfocom.layout.LC
//import net.miginfocom.swing.MigLayout
//import com.linqingying.gitcode.api.data.GCCommit
//import com.linqingying.gitcode.api.data.GCUser
//import com.linqingying.gitcode.i18n.GitCodeBundle
//import com.linqingying.gitcode.pullrequest.comment.convertToHtml
//import com.linqingying.gitcode.pullrequest.ui.details.model.impl.GCPRDetailsViewModel
//import javax.swing.JComponent
//import javax.swing.JPanel
//
//internal object GCPRDetailsComponentFactory {
//
//  fun create(
//    scope: CoroutineScope,
//    project: Project,
//    detailsVm: GCPRDetailsViewModel,
//    commitFilesBrowserComponent: JComponent
//  ): JComponent {
//    val actionGroup = ActionManager.getInstance().getAction("GitCode.PullRequest.Details.Popup") as ActionGroup
//    val title = CodeReviewDetailsTitleComponentFactory.create(scope, detailsVm, GitCodeBundle.message("open.on.gitcode.action"), actionGroup,
//                                                              htmlPaneFactory = { SimpleHtmlPane() })
//    val timelineLink = ActionLink(CollaborationToolsBundle.message("review.details.view.timeline.action")) {
//      showTimelineAction(it.source as JComponent)
//    }
//
//    val commitsAndBranches = JPanel(MigLayout(LC().emptyBorders().fill(), AC().gap("push"))).apply {
//      isOpaque = false
//      add(CodeReviewDetailsCommitsComponentFactory.create(scope, detailsVm.changesVm) { commit: GCCommit ->
//        createCommitsPopupPresenter(commit, detailsVm.securityService.ghostUser)
//      })
//      add(CodeReviewDetailsBranchComponentFactory.create(scope, detailsVm.branchesVm))
//    }
//    val statusChecks = GCPRStatusChecksComponentFactory.create(scope, project,
//                                                               detailsVm.statusVm,
//                                                               detailsVm.reviewFlowVm,
//                                                               detailsVm.securityService,
//                                                               detailsVm.avatarIconsProvider)
//    val actionsComponent = GCPRDetailsActionsComponentFactory.create(scope, project, detailsVm.reviewRequestState, detailsVm.reviewFlowVm)
//
//    return JPanel(MigLayout(
//      LC()
//        .emptyBorders()
//        .fill()
//        .flowY()
//        .noGrid()
//        .hideMode(3)
//    )).apply {
//      isOpaque = false
//
//      add(ReviewDetailsUIUtil.createTitlePanel(title, timelineLink),CC().growX().gap(ReviewDetailsUIUtil.TITLE_GAPS))
//      add(commitsAndBranches, CC().growX().gap(ReviewDetailsUIUtil.COMMIT_POPUP_BRANCHES_GAPS))
//      add(CodeReviewDetailsCommitInfoComponentFactory.create(scope, detailsVm.changesVm.selectedCommit,
//                                                             commitPresentation = { commit ->
//                                                               createCommitsPopupPresenter(commit, detailsVm.securityService.ghostUser) {
//                                                                 it.convertToHtml(project)
//                                                               }
//                                                             },
//                                                             htmlPaneFactory = { SimpleHtmlPane() }),
//          CC().growX().gap(ReviewDetailsUIUtil.COMMIT_INFO_GAPS))
//      add(commitFilesBrowserComponent, CC().grow().shrinkPrioY(200))
//      add(statusChecks, CC().growX().gap(ReviewDetailsUIUtil.STATUSES_GAPS).maxHeight("${ReviewDetailsUIUtil.STATUSES_MAX_HEIGHT}"))
//      add(actionsComponent, CC().growX().pushX().gap(ReviewDetailsUIUtil.ACTIONS_GAPS).minHeight("pref"))
//
//      PopupHandler.installPopupMenu(this, actionGroup, ActionPlaces.POPUP)
//    }
//  }
//
//  private fun showTimelineAction(parentComponent: JComponent) {
//    val action = ActionManager.getInstance().getAction("GitCode.PullRequest.Timeline.Show") ?: return
//    ActionUtil.invokeAction(action, parentComponent, ActionPlaces.UNKNOWN, null, null)
//  }
//
//  private fun createCommitsPopupPresenter(
//    commit: GCCommit,
//    ghostUser: GCUser,
//    issueProcessor: ((String) -> String)? = null
//  ) = CommitPresentation(
//    titleHtml = if (issueProcessor != null) issueProcessor(commit.messageHeadlineHTML) else commit.messageHeadlineHTML,
//    descriptionHtml = if (issueProcessor != null) issueProcessor(commit.messageBodyHTML) else commit.messageBodyHTML,
//    author = (commit.author?.user ?: ghostUser).getPresentableName(),
//    committedDate = commit.committedDate
//  )
//}
